package cn.zyx.sys.web;

import cn.zyx.common.utils.snowflake.SnowflakeIdGenerator;
import cn.zyx.config.shiro.cache.redis.myLock.DistributedLockFactory;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.locks.Lock;

/**
 * 系统测试
 * @author zyx
 */
@RestController
@RequestMapping("/sys/test")
public class SysTestController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private DistributedLockFactory distributedLockFactory;

    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private KafkaTemplate kafkaTemplate;


    /**
     * 测试雪花算法生成的id
     */
    @GetMapping("testSnowFlake")
    public String testSnowFlake() {
        System.out.println(String.valueOf(snowflakeIdGenerator.nextId()));
        return String.valueOf(snowflakeIdGenerator.nextId());
    }

    /**
     * 并发测试redisLock
     */
    @GetMapping("testRedisson")
    public String testRedisson() {
        String stockKey = "stockKey";
        RLock lock = redissonClient.getLock("redisLock");
        lock.lock();
        Integer count = 0;
        try {
            // 从缓存获取库存数量
            Object o = redisTemplate.opsForValue().get(stockKey);
            if (o == null) {
                // 从数据库查询
                count = 1000;
            } else {
                count = Integer.valueOf((String) o);
            }
            // 判断库存是否足够
            if (count > 0) {
                // 更新缓存
                redisTemplate.opsForValue().set(stockKey,String.valueOf(--count));
                System.out.println("扣减库存：商品剩余数量：" + count);
                return "扣减库存：商品剩余数量：" + count;
            } else {
                System.out.println("商品买完了！！！");
                return "商品买完了！！！";
            }
        } finally {
            // 是否加锁并且是否是当前线程
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }


    /**
     * 并发测试redisLock
     */
    @GetMapping("testRedisLock")
    public String testRedisLock() {
        String stockKey = "stockKey";
        Lock lock = distributedLockFactory.getDistributedLock("redis");
        lock.lock();
        Integer count = 0;
        try {
            // 从缓存获取库存数量
            Object o = redisTemplate.opsForValue().get(stockKey);
            if (o == null) {
                // 从数据库查询
                count = 1000;
            } else {
                count = Integer.valueOf((String) o);
            }
            // 检查可重入锁 是否成功
            //check();
            // 续期测试
            //TimeUnit.SECONDS.sleep(40);
            // 判断库存是否足够
            if (count > 0) {
                // 更新缓存
                redisTemplate.opsForValue().set(stockKey,String.valueOf(--count));
                System.out.println("扣减库存：商品剩余数量：" + count);
                return "扣减库存：商品剩余数量：" + count;
            } else {
                System.out.println("商品买完了！！！");
                return "商品买完了！！！";
            }
        } finally {
            lock.unlock();
        }
    }

    /**
     * 检查可重入锁
     */
    public void check() {
        Lock lock = distributedLockFactory.getDistributedLock("redis");
        lock.lock();
        try {
            System.out.println("----------------测试可重入锁-----------");
            // 续期测试
            // TimeUnit.SECONDS.sleep(40);
        } finally {
            lock.unlock();
        }
    }

    /**
     * kafkaTest
     */
    @GetMapping("kafkaTest")
    public String kafkaTest() {

        try {
            kafkaTemplate.send("test","ssss");
        } catch (Exception e) {

        }

        return "发送成功";
    }
}
